package com.example.user.UserIntegral.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.user.UserIntegral.entity.Integral;
import com.example.user.UserIntegral.mapper.IntegarlMapper;
import com.example.user.UserIntegral.service.impl.IntegralService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequestMapping("integral")
public class IntegralController {
    @Resource
    private IntegralService integralService;

    @Autowired
    private IntegarlMapper integarlMapper;

    /**
     * 根据openid查询用户积分情况
     *
     * @param integral
     * @return
     */
    @GetMapping("/like")
    public List<Integral> selectList(Integral integral) {
        List<Integral> list = integralService.serviceList(integral);
        return list;
    }

    /**
     * 根据openid 添加下单活动数量和总和
     *
     * @param openid
     * @param activity
     * @param sum
     * @return
     */
    @GetMapping("/activity")
    public boolean integral_update(String openid, Integer activity, Integer consumption) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        Integral integral = new Integral();
        integral.setActivity(activity);
        updateWrapper.eq("openid", openid);
        return integralService.update(integral, updateWrapper);
    }


    /**
     * 根据openid 添加优惠数量和总和
     *
     * @param openid
     * @param consumption
     * @param sum
     * @return
     */
    @GetMapping("/consumption{openid}{consumption}")
    public boolean consumption(String openid, Integer consumption, Integer sum) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        Integral integral = new Integral();
        integral.setConsumption(consumption);
        updateWrapper.eq("openid", openid);
        return integralService.update(integral, updateWrapper);
    }

    /**
     * 添加积分    1 消费积分 2 活动积分
     *
     * @param integral
     * @return
     */
    @PostMapping("/add")
    public Boolean add(@RequestBody Integral integral) {
        String updateTime = LocalDateTime.now() + "";
        integral.setTime(updateTime.substring(0, 10));
        integral.setOrderid(DigestUtils.md5Hex(integral.getOrderid() + updateTime.substring(0, 10)));
        return integralService.save(integral);
    }
}
